package com.laijiale.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.laijiale.dto.NoticeInsertDTO;
import com.laijiale.dto.NoticePageDTO;
import com.laijiale.dto.NoticeUpdateDTO;
import com.laijiale.entity.*;
import com.laijiale.entity.table.CourseTableDef;
import com.laijiale.entity.table.NoticeTableDef;
import com.laijiale.exception.ServiceException;
import com.laijiale.vo.NoticeSimpleListVO;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.relation.RelationManager;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.laijiale.entity.Notice;
import com.laijiale.mapper.NoticeMapper;
import com.laijiale.service.NoticeService;
import org.aspectj.weaver.ast.Not;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

import static com.laijiale.entity.table.CourseTableDef.COURSE;
import static com.laijiale.entity.table.NoticeTableDef.NOTICE;


/**
 * 通知表 服务层实现。
 *
 * @author laijiale
 * @since 2024-12-22
 */
@Service
@CacheConfig(cacheNames = "notice")
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>  implements NoticeService{


    @CacheEvict(allEntries = true)
    @Override
    public boolean insert(NoticeInsertDTO dto) {
        Notice notice= BeanUtil.copyProperties(dto,Notice.class);
       notice.setCreated(LocalDateTime.now());
       notice.setUpdated(LocalDateTime.now());
        return mapper.insert(notice)>0;
    }
    @Cacheable(key = "#p0",condition = "#p0 !=null",unless = "#result ==null")
    @Override
    public Notice select(Long id) {
        return mapper.selectOneWithRelationsById(id);
    }


    @Cacheable(key = "#root.methodName",
            unless = "#result == null"
    )
    @Override
    public List<Notice> top(Long N) {
        List<Notice> notices= QueryChain.of(mapper).limit(N).list();
        return notices;
    }

    @Cacheable(key = "#root.methodName",
            unless = "#result == null"
    )
    @Override
    public List<NoticeSimpleListVO> simpleList() {

        return QueryChain.of(mapper).withRelations().listAs(NoticeSimpleListVO.class);
    }


    @Cacheable(key = "#root.methodName + ':' + #p0.toString()",
            condition ="#p0 != null",
            unless = "#result == null")
    @Override
    public Page<Notice> page(NoticePageDTO dto) {
        QueryChain<Notice> queryChain = QueryChain.of(mapper).orderBy(NOTICE.IDX.asc(), NOTICE.ID.desc());
        return queryChain.withRelations().page(new Page<>(dto.getPageNum(), dto.getPageSize()));

    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean update(NoticeUpdateDTO dto) {

        // 组装 entity 实体类
        Notice notice = BeanUtil.copyProperties(dto, Notice.class);
        // 设置 entity 默认值
        notice.setUpdated(LocalDateTime.now());
        return UpdateChain.of(notice).where(NOTICE.ID.eq(notice.getId())).update();
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public boolean delete(Long id) {
        return mapper.deleteById(id) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public boolean deleteBatch(List<Long> ids) {
        return mapper.deleteBatchByIds(ids) > 0;
    }
}
